/*
 * Copyright (c) 2014, KJFrameForAndroid 张涛 (kymjs123@gmail.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.imay.videorecord.utils;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;

public class DensityUtils {
	
	static int screenW = 0;
	static int screenH = 0;

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 sp
     */
    public static int px2sp(Context context, float pxValue) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 sp 的单位 转成为 px
     */
    public static int sp2px(Context context, float spValue) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }


    /**
     * 获取屏幕宽度
     */
    public static int getScreenW(Activity aty) {
    	if (screenW == 0){
            DisplayMetrics dm = new DisplayMetrics();
            dm = aty.getResources().getDisplayMetrics();
            int w = dm.widthPixels;
            screenW = w;
            // int w = aty.getWindowManager().getDefaultDisplay().getWidth();
    	}
    	return screenW;
    }

    /**
     * 获取屏幕高度
     */
    public static int getScreenH(Activity aty) {
    	if (screenH == 0){
            DisplayMetrics dm = new DisplayMetrics();
            dm = aty.getResources().getDisplayMetrics();
            int h = dm.heightPixels;
            screenH = h;
            // int w = aty.getWindowManager().getDefaultDisplay().getWidth();
    	}
        return screenH;
    }
    /**
  	 * 获得状态栏的高度
  	 */
  	public static int getStatusHeight(Context context) {

  		int statusHeight = -1;
  		try {
  			Class<?> clazz = Class.forName("com.android.internal.R$dimen");
  			Object object = clazz.newInstance();
  			int height = Integer.parseInt(clazz.getField("status_bar_height")
  					.get(object).toString());
  			statusHeight = context.getResources().getDimensionPixelSize(height);
  		} catch (Exception e) {
  			e.printStackTrace();
  		}
  		return statusHeight;
  	}
}